package com.serverTechnology.teamsGroups.controller;

import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import com.serverTechnology.teamsGroups.constants.FixedNames;
import com.serverTechnology.teamsGroups.entity.dto.StepsDrawingDto;
import com.serverTechnology.teamsGroups.service.ITeStepsDrawingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/drawing")
@ApiResource(name = "工步所需图纸接口")
@Api(tags = "工步所需图纸管理")
public class TeStepsDrawingController {

    @Autowired
    private ITeStepsDrawingService iTeStepsDrawingService;
    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传文件并返回Base64编码及文件名")
    public ResponseData<Map<String, String>> uploadFile(@RequestPart("file") MultipartFile file) {
        if (!file.getContentType().equals(FixedNames.PDF)) {
            throw new RuntimeException("文件格式错误");
        }

        try {
            byte[] fileBytes = file.getBytes();
            String base64EncodedFile = Base64.getEncoder().encodeToString(fileBytes);
            String originalFileName = file.getOriginalFilename();

            Map<String, String> response = new HashMap<>();
            response.put("fileName", originalFileName);
            response.put("base64File", base64EncodedFile);

            return new SuccessResponseData<>(response);
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败", e);
        }
    }

    @PostMapping("/addDrawing")
    @ApiOperation(value = "添加工步所需图纸")
    public ResponseData addDrawing(@Validated @RequestBody StepsDrawingDto stepsDrawingDto) {
        iTeStepsDrawingService.addDrawing(stepsDrawingDto);
        return new SuccessResponseData();
    }

//    /**
//     * 添加工步所需图纸
//     * @param file
//     * @return
//     */
//    @PostMapping("/add")
//    @ApiOperation(value = "添加工步所需图纸")
//    public ResponseData addDrawing( @Validated StepsDrawingDto stepsDrawingDto,@RequestPart("file")MultipartFile file) {
//        iTeStepsDrawingService.addDrawing(file, stepsDrawingDto);
//        return new SuccessResponseData();
//    }

    /**
     * 查询工步所需图纸
     * @param id
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "查询工步所需图纸")
    public void pageDrawing(@RequestParam Long id, HttpServletResponse response) {
        iTeStepsDrawingService.pageDrawing(id,response);
    }
}
















